<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Cookie;
use think\Request;
use app\index\model\Nav;
use app\index\service\OrderService;
use app\index\service\CourseService;
use app\index\model\Order as OrderModel;
use alipay\pagepay\service\AlipayTradeService;




class Order  extends Common
{
	public function __construct(Request $request)
	{
		parent::__construct();
		$this->config = array (
			'app_id' => "2021004123616781",
			'merchant_private_key' => config('merchant_private_key'),
			'notify_url' => "https://www.atxunda.com/orders/alinotify.html",
			'return_url' => "https://www.atxunda.com/orders/alireturn.html",
			'charset' => "UTF-8",
			'sign_type'=>"RSA2",
			'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
			'alipay_public_key' => config('alipay_public_key'),
		);
	}

	public function index(Request $request){
		$param = $request->param();
		$param['pack'] = $param['pack'] ?: 2;
		$user = user();
		if (!$user) $this->redirect('/login.html?redirect=http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
		$data = Nav::getOneData($param['nav_id']);
		$data = CourseService::getNavPrice($data, $param['pack']);
		if (!$param['nav_id'] || !$data) die($this->checkParamId());
		return $this->fetch('',[
			'data' => $data,
			'param' => $param,
			'title' => '订单支付页-'.$data['nav_name'],
		]);
	}

	public function create(Request $request){
		$param = $request->param();
		$user = user();
		if (!$user) return json(['code'=>400,'msg'=>'未登录']);
		$orderId = OrderService::addOrder($param);
		return json(['code'=>200,'id'=>$orderId]);
	}

	public function shows(Request $request){
		$param = $request->param();
		$data = OrderModel::getDetail($param['id']);
		if (!$param['id'] || !$data) die($this->checkParamId());
		if ( $data['action'] == 1){
			header("Content-type:text/html;charset=utf-8");
			$config = $this->config;
			var_dump(OrderService::disposeAlipay($config, $data));
		}else{
			$url = OrderService::wxpayQRCode($data);
			return $this->fetch('wxpay',[
				'url' => $url,
				'data' => $data,
			]);
		}
	}


	public function alinotify(Request $request){
		$param = $request->param();
		$config = $this->config;
		if (!$param['out_trade_no']) return [];
		echo OrderService::alipayNotify($config, $param);
	}

	public function alireturn(Request $request){
		$param = $request->param();
		$request = Request::instance();
		$order = OrderModel::getDetail($param['out_trade_no']);
		if($order['status'] == 1) {
			$this->redirect('/course/'.$order['nav_id'].'.html');
		}else {
			$this->success('支付失败','/goods/'.$order['nav_id'].'.html');
		}
	}

	public function wxnotify(Request $request){
		$param = $request->param();
		if (!$param['id']) return [];
		OrderService::wxNotify($param);
	}

	public function wxreturn(Request $request){
		$param = $request->param();
		if (!$param['id']) return [];
		$order = OrderModel::getDetail($param['id']);
		return json(['status'=>OrderService::wxReturn($param),'id'=>$order['nav_id']]);
	}
}